package com.purong.purong.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.bean.LoginBean;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wenjiezeng on 2017/10/8.
 * 修改用户昵称
 */
public class NickEditActivity extends BaseActivity{
    private EditText nickEd;
    private TextView confirm;
    private String nick;


    @Override
    public void initBundle() {
        nick=getIntent().getStringExtra("NICK");
    }

    @Override
    public int getRootViewId() {
        return R.layout.avtivity_edit_nick;
    }

    public void initView(){
        showBack();
        showTitle("修改昵称");
        confirm=(TextView)findViewById(R.id.confirm);
        nickEd=(EditText)findViewById(R.id.nick_ed);
        if (nick!=null){
            nickEd.setText(nick);
            confirm.setBackgroundResource(R.drawable.button_blue_radius5);
        }
        nickEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    confirm.setBackgroundResource(R.drawable.button_gray_radius5);
                } else {
                    confirm.setBackgroundResource(R.drawable.button_blue_radius5);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {

    }

    private void editNick(String name){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("field","nick");
        params.put("value",name);
        request(Contant.SET_FIELD, params, true,true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.SET_FIELD)){
            if (responseInfo.isRequestOk){
                if (PuRongApp.getLoginInfo(this)!=null){
                    LoginBean bean=PuRongApp.getLoginInfo(this);
                    bean.setNick(responseInfo.params.get("value"));
                    bean.save(this);
                }
                showToast("修改成功");
                this.finish();
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (nickEd.getText().toString().trim().equals("")){
                    showToast("昵称不能为空");
                    return;
                }

                editNick(nickEd.getText().toString().trim());
                break;
        }
    }
}
